package com.ymatou.iapi.delivery.testcase;

import java.util.List;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.delivery.parameter.ModifyObservationModeReq;
import com.ymatou.iapi.delivery.service.ModifyObservationModeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.BillnoRule;
import com.ymttest.database.sqlwapper.BillnoRuleWapper;
/**
*修改拦截模式接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_modifyObservationMode {
	private static ModifyObservationModeReq bean;
	private static ModifyObservationModeCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改拦截模式接口");
	}

	@Before
	public void caseUp() {
		bean = new ModifyObservationModeReq();
		call = new ModifyObservationModeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 
	 * @param expressCode
	 * @param rule
	 * @param isObservationMode	是否为观察模式
	 * @throws Exception
	 */
	public static void callModifyObservationMode(String expressCode,
			String rule, boolean isObservationMode) throws Exception {

		ModifyObservationModeReq bean = new ModifyObservationModeReq();
		ModifyObservationModeCall call = new ModifyObservationModeCall();

		bean.setObservationMode(isObservationMode);
		bean.setRegexRule(rule);
		bean.setYmtExpressCode(expressCode);

		call.setData(false, bean);
		call.callService();

		//缓存60s后生效
		Thread.sleep(80000);
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyObservationMode_001() {
		Logger.start(true, "设置拦截模式为 观察这模式");
		try {

			BillnoRuleWapper billnoRuleWapper = new BillnoRuleWapper();

			List<BillnoRule> rules = billnoRuleWapper.selectBillnoRule(
					"AUTOTEST%", null);

			BillnoRule billnoRule = rules
					.get(new Random().nextInt(rules.size()));

			bean.setObservationMode(true);
			bean.setRegexRule(billnoRule.getRegex_rule());
			bean.setYmtExpressCode(billnoRule.getYmt_expresscode());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals(call.getString("IsSuccess"), "true",
					"校验IsSuccess");

			Logger.verifyIsNull(call.getString("Message"), "校验Message");

			BillnoRule rule = new BillnoRuleWapper().selectBillnoRule(
					bean.getYmtExpressCode(), bean.getRegexRule()).get(0);

			Logger.verifyEquals(bean.isObservationMode(),
					rule.getObservation_mode(), "校验ObservationMode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyObservationMode_002() {
		Logger.start(true, "设置拦截模式为 拦截模式");
		try {

			BillnoRuleWapper billnoRuleWapper = new BillnoRuleWapper();

			List<BillnoRule> rules = billnoRuleWapper.selectBillnoRule(null,
					null);

			BillnoRule billnoRule = rules.get(new Random().nextInt(389));

			bean.setObservationMode(false);
			bean.setRegexRule(billnoRule.getRegex_rule());
			bean.setYmtExpressCode(billnoRule.getYmt_expresscode());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals(call.getString("IsSuccess"), "true",
					"校验IsSuccess");

			Logger.verifyIsNull(call.getString("Message"), "校验Message");

			BillnoRule rule = new BillnoRuleWapper().selectBillnoRule(
					bean.getYmtExpressCode(), bean.getRegexRule()).get(0);

			Logger.verifyEquals(bean.isObservationMode(),
					rule.getObservation_mode(), "校验ObservationMode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyObservationMode_003() {
		Logger.start(true, "ymtExpressCode为 null");
		try {

			BillnoRuleWapper billnoRuleWapper = new BillnoRuleWapper();

			List<BillnoRule> rules = billnoRuleWapper.selectBillnoRule(null,
					null);

			BillnoRule billnoRule = rules.get(new Random().nextInt(389));

			bean.setObservationMode(true);
			bean.setRegexRule(billnoRule.getRegex_rule());
			//bean.setYmtExpressCode(billnoRule.getYmt_expresscode());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals(call.getString("IsSuccess"), "false",
					"校验IsSuccess");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					call.getString("errorCode"), "校验errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyObservationMode_004() {
		Logger.start(true, "regexRule为 null");
		try {

			BillnoRuleWapper billnoRuleWapper = new BillnoRuleWapper();

			List<BillnoRule> rules = billnoRuleWapper.selectBillnoRule(
					"AUTOTEST%", null);

			BillnoRule billnoRule = rules
					.get(new Random().nextInt(rules.size()));

			bean.setObservationMode(true);
			//bean.setRegexRule(billnoRule.getRegex_rule());
			bean.setYmtExpressCode(billnoRule.getYmt_expresscode());

			call.setData(bean);
			call.callService();

			Logger.verifyEquals(call.getString("IsSuccess"), "false",
					"校验IsSuccess");

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					call.getString("errorCode"), "校验errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}